package com.wh.weibo.service;

import com.wh.weibo.mapper.UserMapper;
import com.wh.weibo.model.ServiceResult;
import com.wh.weibo.model.User;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sun.misc.BASE64Encoder;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @author WeiHong
 * @date 2019/7/8 15:53
 */
@Service
public class UserService {
    @Resource
    private UserMapper userMapper;

    /**
     * 用户登陆
     *
     * @param loginName
     * @param password
     * @return
     */
    public ServiceResult login(String loginName, String password) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        ServiceResult serviceResult = new ServiceResult();
        User user = userMapper.selectByLoginName(loginName);
        if (null == user){
            serviceResult.setSuccess(false);
            serviceResult.setData("用户名或者密码错误");
            return serviceResult;
        }
        String encryPassword = encodeByMd5(password);
        if (!encryPassword.equals(user.getPassword())){
            serviceResult.setSuccess(false);
            serviceResult.setData("用户名或者密码错误");
            return serviceResult;
        }
        serviceResult.setSuccess(true);
        serviceResult.setData(user);
        return serviceResult;
    }


    /**
     * 用户注册服务
     *
     * @param loginName 用户名称
     * @param password  用户登陆密码
     * @param nickName
     * @param gender
     * @param age
     * @param avatarUrl
     */
    @Transactional(rollbackFor = Exception.class)
    public ServiceResult register(String loginName, String password, String nickName, Integer gender, Integer age, String avatarUrl) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        ServiceResult serviceResult = new ServiceResult();
        User user = new User();
        user.setLoginName(loginName);
        user.setPassword(encodeByMd5(password));
        user.setNickName(nickName);
        user.setGender(gender.byteValue());
        user.setAge(age);
        user.setAvatarUrl(avatarUrl);

        try {
            userMapper.insertSelective(user);
        } catch (DuplicateKeyException dupEx) {
            serviceResult.setSuccess(false);
            serviceResult.setErrorMsg("用户登录名重复");
        }
        return serviceResult;
    }

    /**
     * MD5加秘
     *
     * @param str
     * @return
     * @throws NoSuchAlgorithmException
     * @throws UnsupportedEncodingException
     */
    private String encodeByMd5(String str) throws NoSuchAlgorithmException, UnsupportedEncodingException {
        MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        BASE64Encoder base64Encoder = new BASE64Encoder();
        String encodedStr = base64Encoder.encode(messageDigest.digest(str.getBytes("utf-8")));
        return encodedStr;
    }
}
